﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using usedcartrade.ViewModels;
using usedcartrade.Models;
using usedcartrade.DataAccessLayer;
using usedcartrade.Repositories;
using System.Data.Entity;

namespace usedcartrade.Controllers
{
    public class HomeController : Controller
    {
        private UsedCarERPDAL db = new UsedCarERPDAL();

        // GET: Home
        public ActionResult Index()
        {
            CarListViewModel carListViewModel = new CarListViewModel();
            CarBusinessLayer carBal = new CarBusinessLayer();
            List<Car> cars = carBal.GetCars();
            List<CarViewModel> carviewmodels = new List<CarViewModel>();

            foreach(Car car in cars)
            {
                CarViewModel cvm = new CarViewModel();
                cvm.ID = car.ID;
                cvm.UserID =car.UserID;
                cvm.carname = car.CarName;
                cvm.cartypename = car.cartype.typename;
                cvm.carbrandname = car.carbrand.BrandName;
                cvm.caruser = car.user.userDetails.UserName;
                carviewmodels.Add(cvm);
            }
            carListViewModel.Cars = carviewmodels;
            return View("Index",carListViewModel);
        }

        public ActionResult IfLogin()
        {
            if(Session["IsLogin"]==null||Convert.ToBoolean(Session["IsLogin"])==false)
            {
                return PartialView("LogReg");
            }else
            {
                return PartialView("HasLogged");
            }
        }

        public ActionResult OrdUserFunc()
        {
            if(Session["IsLogin"]==null||Convert.ToBoolean(Session["IsLogin"])==false)
            {
                return new EmptyResult();
            }else
            {
                return PartialView("UserFunc");
            }

        }
        public ActionResult AdminFunc()
        {
            if(Session["IsAdmin"]==null||Convert.ToBoolean(Session["IsAdmin"])==false)
            {
                return new EmptyResult();
            }else
            {
                return PartialView("AdminFunc");
            }

        }

    }
}